/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.paint;

import com.nokia.example.paint.views.DrawArea;
import com.nokia.example.paint.views.Splash;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;

/*
 * Main class for starting the application, pausing it, and exiting.
 */
public class Main
    extends MIDlet
    implements CommandListener {

    private Display display;
    private Splash splash;
    private DrawArea view;
    private static Main self;
    private Alert progress = new Alert("Saving", "Saving image", null, AlertType.INFO);
    private Alert error = new Alert("Error!", "Error happened.", null, AlertType.ERROR);
    private Alert exitPrompt = new Alert("Exit", "You have unsaved changes. Would you like to save them before you quit?", null, AlertType.CONFIRMATION);
    private Command yes = new Command("Save and quit", Command.OK, 1);
    private Command no = new Command("No", Command.EXIT, 2);
    private Command cancel = new Command("Cancel", Command.CANCEL, 3);

    public static Main getInstance() {

        return self;
    }

    /**
     * Start the application.
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    public void startApp() {
        // Hide virtual keyboard if the device has one.
        try {
            Class.forName("com.nokia.mid.ui.VirtualKeyboard");
            com.nokia.mid.ui.VirtualKeyboard.hideOpenKeypadCommand(true);
            com.nokia.mid.ui.VirtualKeyboard.suppressSizeChanged(false);
        }
        catch (ClassNotFoundException e) {
            // Ignore the exception.
        }

        if (display == null) {
            display = Display.getDisplay(this);
            self = this;
            splash = new Splash();
            display.setCurrent(splash);
        }
    }

    public void initPaint() {
        view = new DrawArea();
        showDrawing();
        splash = null;
        progress.setTimeout(3600000);
        error.setTimeout(Alert.FOREVER);
        error.setCommandListener(this);
        Gauge g = new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        progress.setIndicator(g);
        exitPrompt.addCommand(yes);
        exitPrompt.addCommand(no);
        exitPrompt.addCommand(cancel);
        exitPrompt.setCommandListener(this);
    }

    public void showDrawing() {
        display.setCurrent(view);
    }

    public DrawArea getDrawArea() {
        return view;
    }

    public void showProgressAlert() {
        display.setCurrent(progress);
    }

    public void showError(String errorMessage) {
        error.setString(errorMessage);
        display.setCurrent(error, view);

    }

    public void hideProgressAlert() {
        if (progress.isShown()) {
            display.setCurrent(view);
        }
    }

    public void showExitPrompt() {
        display.setCurrent(exitPrompt);
    }

    public synchronized void updateProgressText(String newtext) {
        if (newtext != null) {
            progress.setString(newtext);
        }
    }

    /**
     * Must be implemented by CommandListener. These commands concern only the exit dialog.
     * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
     * @param cmnd Command object
     * @param dsplbl Displayable that sent the Command
     */
    public void commandAction(Command cmnd, Displayable dsplbl) {
        if (cmnd == yes) {
            // Exit dialog reply was yes - the user wants to save
            // the image before quitting.
            showDrawing();
            getDrawArea().showSaveDialog(true);
        }
        else if (cmnd == no) {
            exit();
        }
        else if (cmnd == cancel) {
            showDrawing();
        }
        else if (cmnd == Alert.DISMISS_COMMAND) {
            display.setCurrent(view);
        }
    }

    /**
     * Pauses the application.
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    public void pauseApp() {
        // Nothing to do here.
    }

    /**
     * Exits the application.
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     * @param unconditional Should the MIDlet cleanup and release all resources.
     */
    public void destroyApp(boolean unconditional) {
        // Nothing to do here.
    }

    public void exit() {
        destroyApp(true);
        notifyDestroyed();
    }
}
